﻿using System;
using System.Data;
using System.Collections.Generic;
using SERP.DAO;
namespace SERP.Controllers
{
    public class SalaryRuleItemsController : SalaryRuleItemsControllerBase
    {
        public SalaryRuleItemsController()
            : base()
        {
        }
        private DataTable _Table;
        private SalaryRuleItemsEntity oSalaryRuleItems;
        public SalaryRuleItemsEntity SalaryRuleItemsObject
        {
            get
            {
                return oSalaryRuleItems;
            }
            set
            {
                oSalaryRuleItems = value;
            }
        }
        public DataTable Table
        {
            get
            {
                return _Table;
            }
            set
            {
                _Table = value;
            }
        }
        public void LoadAllDSSalaryRuleItems(int salaryRuleId)
        {
            DataSet ds = Service.SelectAllDSFilter("SalaryRuleId="+salaryRuleId.ToString());
            _Table = ds.Tables[0];
            AddIndexToTable(_Table);
        }
        public void AddIndexToTable()
        {
            AddIndexToTable(_Table);
        }
        public void ResetTableIndex()
        {
            ResetTableIndex(_Table);
        }
        public void LoadObject(Int32 SalaryRuleItemId)
        {
            SalaryRuleItemsEntity oLoad = FindSalaryRuleItems(SalaryRuleItemId);
            if (oLoad == null)
            {
                LoadAllSalaryRuleItems(true);
                oSalaryRuleItems = FindSalaryRuleItems(SalaryRuleItemId);
            }
            else
            {
                oSalaryRuleItems = oLoad;
            }
        }
        public bool AddNew()
        {
            if (oSalaryRuleItems == null) return false;
            bool result = Service.Save(oSalaryRuleItems);
            if (result)
            {
                AddObjectToCache(oSalaryRuleItems);
            }
            return result;
        }
        public bool Update()
        {
            if (oSalaryRuleItems == null) return false;
            return Service.Update(oSalaryRuleItems);
        }
        public bool Delete()
        {
            if (oSalaryRuleItems == null) return false;
            bool retvalue = Service.Delete(oSalaryRuleItems);
            if (retvalue)
            {
                RemoveObjectFromCache(oSalaryRuleItems);
            }
            return retvalue;
        }
        public bool DeleteByID(Int32 SalaryRuleItemId)
        {
            bool retvalue = Service.DeleteByID(SalaryRuleItemId);
            if (retvalue)
            {
                SalaryRuleItemsEntity oDeleted = FindSalaryRuleItems(SalaryRuleItemId);
                RemoveObjectFromCache(oDeleted);
            }
            return retvalue;
        }
        public void ResetObject()
        {
            this.oSalaryRuleItems = new SalaryRuleItemsEntity();
        }
        public void MapObjectToDataRow(DataRow row)
        {
            this.MapObjectToDataRow(this.SalaryRuleItemsObject, row);
        }
        public SalaryRuleItemsEntity LoadSalaryRuleItems(Int32 SalaryRuleItemId)
        {
            SalaryRuleItemsEntity o = Service.SelectOne(SalaryRuleItemId);
            return o;
        }
        #region Cached
        private static List<SalaryRuleItemsEntity> _ListSalaryRuleItems;
        private static bool _AllowCaching = true;
        public static bool AllowCaching
        {
            get { return _AllowCaching; }
            set { AllowCaching = value; }
        }
        public static void ClearCache()
        {
            if (!_AllowCaching) return;
            Cache.Remove(CACHE_KEY_DS);
            Cache.Remove(CACHE_KEY_LS);
        }
        private const string CACHE_KEY_DS = "SalaryRuleItemsDS";
        private const string CACHE_KEY_LS = "SalaryRuleItemsLS";
        private void AddObjectToCache(SalaryRuleItemsEntity newObject)
        {
            if (!AllowCaching) return;
            GetCacheList().Add(newObject);
        }
        private void RemoveObjectFromCache(SalaryRuleItemsEntity oRemove)
        {
            if (!AllowCaching) return;
            if (oRemove != null)
                GetCacheList().Remove(oRemove);
        }
        private static List<SalaryRuleItemsEntity> GetCacheList()
        {
            if (Cache == null) return default(List<SalaryRuleItemsEntity>);
            List<SalaryRuleItemsEntity> ls = Cache.Get<List<SalaryRuleItemsEntity>>(CACHE_KEY_LS);
            if (ls == null)
            {
                ls = new List<SalaryRuleItemsEntity>();
                AddCache(CACHE_KEY_LS, ls);
            }
            return ls;
        }
        public List<SalaryRuleItemsEntity> ListSalaryRuleItems
        {
            get
            {
                return LoadAllSalaryRuleItems(false);
            }
        }
        public static List<SalaryRuleItemsEntity> ListSalaryRuleItemsStatic
        {
            get
            {
                LoadAllSalaryRuleItems(false);
                return _ListSalaryRuleItems;
            }
        }
        public SalaryRuleItemsEntity FindSalaryRuleItems(Int32 SalaryRuleItemId)
        {
            if (AllowCaching)
            {
                _ListSalaryRuleItems = GetCacheList();
            }
            SalaryRuleItemsEntity oResult = _ListSalaryRuleItems.Find(delegate(SalaryRuleItemsEntity oFind)
            {
                bool result = true;
                result &= oFind.SalaryRuleItemId.Equals(SalaryRuleItemId);
                return result;
            });
            return oResult;
        }
        public static List<SalaryRuleItemsEntity> LoadAllSalaryRuleItems(bool clearCache)
        {
            _ListSalaryRuleItems = new List<SalaryRuleItemsEntity>();
            if (AllowCaching)
            {
                _ListSalaryRuleItems = GetCacheList();
            }
            if (_ListSalaryRuleItems == null || _ListSalaryRuleItems.Count == 0)
                clearCache = true;
            if (!clearCache) return _ListSalaryRuleItems;
            _ListSalaryRuleItems = new List<SalaryRuleItemsEntity>(Service.SelectAll());
            if (AllowCaching)
                AddCache(CACHE_KEY_LS, _ListSalaryRuleItems);
            return _ListSalaryRuleItems;
        }
        #endregion
    }
}
